package test.javamail;

import java.io.File;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import cn.itcast.mail.AttachBean;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;

public class SendMail {

    public static void main(String[] args) throws Exception {
        sendNormalMail();
    }

    /**
     * @description 发送邮件
     * @throws AddressException
     * @throws MessagingException
     */
    public static void sendNormalMail() throws AddressException, MessagingException {
        // 指定邮件服务器
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.sina.com");
        props.setProperty("mail.smtp.auth", "true");
        // 获取发送权限
        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("13570907717@sina.cn", "123qwert");
            }
        };
        Session session = Session.getInstance(props, auth);

        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("13570907717@sina.cn"));// 设置发件人(显示的发件人,可以瞎写)
        msg.setRecipients(RecipientType.TO, "3400048233@qq.com");// 设置收件人
        msg.setRecipients(RecipientType.CC, "328893769@qq.com");// 设置抄送
        msg.setRecipients(RecipientType.BCC, "13532244566@sina.cn");// 设置暗送

        msg.setSubject("这是主题");
        msg.setContent("这是内容!哈哈哈哈哈", "text/html;charset=utf-8");

        Transport.send(msg);
    }

    /**
     * 发送带有附件的邮件
     * 
     * @description
     * @throws Exception
     */
    public static void sendMailWithFile() throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.sina.com");
        props.setProperty("mail.smtp.auth", "true");
        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("13570907717@sina.cn", "123qwert");
            }
        };
        Session session = Session.getInstance(props, auth);

        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("13570907717@sina.cn"));// 设置发件人
        msg.setRecipients(RecipientType.TO, "328893769@qq.com");// 设置收件人

        msg.setSubject("这是主题");

        // 创建多部分内容
        MimeMultipart list = new MimeMultipart();
        // 正文部分
        MimeBodyPart part1 = new MimeBodyPart();
        part1.setContent("这是内容!哈哈哈哈哈", "text/html;charset=utf-8");
        list.addBodyPart(part1);
        // 附件部分
        MimeBodyPart part2 = new MimeBodyPart();
        part2.attachFile(new File("F:/123.jpg"));// 设置附件的内容
        part2.setFileName(MimeUtility.encodeText("附件.jpg"));// 设置显示的文件名称，其中encodeText用来处理中文乱码问题
        list.addBodyPart(part2);

        msg.setContent(list);
        Transport.send(msg);
    }

    /**
     * @description 用封装好的工具发邮件
     * @throws Exception
     */
    public static void sendMailByUtils() throws Exception {

        Session session = MailUtils.createSession("smtp.sina.com", "13570907717@sina.cn", "123qwert");

        Mail mail = new Mail("13570907717@sina.cn", "3400048233@qq.com, 13532244566@sina.cn", "这是主题", "这里是正文!哈哈哈哈");
        /* 添加附件 */
        AttachBean file1 = new AttachBean(new File("F:/123.jpg"), "附件1.jpg");
        AttachBean file2 = new AttachBean(new File("F:/print.txt"), "附件2.txt");
        mail.addAttach(file1);
        mail.addAttach(file2);

        MailUtils.send(session, mail);
    }
}
